package edu.scau.mis.pos.service.impl;

import edu.scau.mis.core.config.SnowFlakeUtil;
import edu.scau.mis.pos.domain.Order;
import edu.scau.mis.pos.domain.OrderItem;
import edu.scau.mis.pos.domain.VipItem;
import edu.scau.mis.pos.mapper.OrderItemMapper;
import edu.scau.mis.pos.mapper.OrderMapper;
import edu.scau.mis.pos.mapper.VipItemMapper;
import edu.scau.mis.pos.service.IOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Order)表服务实现类
 *
 * @author CJW
 * @since 2024-04-04 00:21:48
 */
@Service("orderService")
public class OrderServiceImpl implements IOrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    private VipItemMapper VipItemMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param orderId 主键
     * @return 实例对象
     */
    @Override
    public Order selectById(Long orderId) {
        return this.orderMapper.selectById(orderId);
    }

    /**
     * 通过ID查询单条数据以完善Payment
     *
     * @param orderId 主键
     * @return 实例对象
     */
    @Override
    public Order selectPayById(Long orderId) {
        return this.orderMapper.selectPayById(orderId);
    }

    /**
     * 查询指定行
     *
     * @param order 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Order> selectList(Order order) {
        return this.orderMapper.selectList(order);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order insert(Order order) {
        order.setCreateTime(new Date());
        this.orderMapper.insert(order);
        return order;
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Transactional
    public Order update(Order order) {
        order.setUpdateTime(new Date());
        // 更新订单信息
        orderMapper.update(order);

        orderMapper.realDeleteOrderItem(order.getOrderId());

        // 更新订单项信息
        List<OrderItem> orderItems = order.getOrderItems();
        if (orderItems != null && !orderItems.isEmpty()) {
            for (OrderItem orderItem : orderItems) {
                orderItem.setOrderId(order.getOrderId()); // 设置订单项的orderId为订单的orderId
                orderItemMapper.insert(orderItem);
            }
        }

        // 确保返回最新更新后的订单信息
        return orderMapper.selectById(order.getOrderId());
    }

    /**
     * 通过主键批量删除数据
     *
     * @param orderId 主键
     * @return 影响记录数
     */
    @Override
    public int deleteByIds(Long[] orderId) {
        return this.orderMapper.deleteByIds(orderId);
    }

    @Override
    public void createOrder(Order order) {
        // 生成订单号
        String orderNo = String.valueOf(SnowFlakeUtil.getSnowFlakeId());
        order.setOrderNo(orderNo);
        // 插入订单
        orderMapper.insert(order);
        Long orderId = order.getOrderId();

        // 设置订单明细中的订单ID
        List<OrderItem> orderItems = order.getOrderItems();
        for (OrderItem item : orderItems) {
            item.setOrderId(orderId);
        }

        // 插入订单明细
        orderMapper.insertOrderItems(orderItems);
    }


    public void deleteOrderItemsAndOrders(Long[] orderIds) {
        // 构建逗号分隔的字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < orderIds.length; i++) {
            sb.append(orderIds[i]);
            if (i < orderIds.length - 1) {
                sb.append(",");
            }
        }

        // 删除订单项数据
        orderMapper.deleteOrderItem(orderIds);

        // 删除订单数据
        orderMapper.deleteByIds(orderIds);
    }

    public void startOrderItemsAndOrders(Long[] orderIds) {
        // 构建逗号分隔的字符串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < orderIds.length; i++) {
            sb.append(orderIds[i]);
            if (i < orderIds.length - 1) {
                sb.append(",");
            }
        }
        // 删除订单数据
        orderMapper.startByIds(orderIds);
    }
    @Override
    public long count(Order order) {
        return orderMapper.count(order);
    }

    @Override
    public List<Order> selectList(Order order, int start, int end) {
        return this.orderMapper.selectList(order);
    }

    @Override
    public List<VipItem> useVip(Long orderId) {
        // 增加使用次数
        this.orderMapper.usevip(orderId);

        Long vipId = this.orderMapper.selectById(orderId).getVipId();
        VipItem vipItem = new VipItem();
        vipItem.setVipId(vipId);

        return this.VipItemMapper.selectList(vipItem);
    }
}
